package cn.b.sky.mes.pp.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.cm.model.WorkCenter;
import cn.b.sky.mes.cm.service.WorkCenterService;
import cn.b.sky.mes.mm.model.PFEP;
import cn.b.sky.mes.mm.model.Requirement;
import cn.b.sky.mes.mm.service.PFEPService;
import cn.b.sky.common.model.DWZResult;
import org.apache.commons.lang3.StringUtils;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.pp.service.RequirementStrategyService;

/**
 * 
 * <strong>Title : ManualRequirementController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.pp.controller<br></strong>
 * <strong>Description : </strong>手工需求控制器<br> 
 * <strong>Create on : 2016年9月18日 下午2:29:04<br></strong>
 * <p>
 * @author zhaoxin01<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/manualRequirement/*")
public class ManualRequirementController extends SecurityController {

  @Resource
  WorkCenterService workCenterService;

  @Resource
  RequirementStrategyService requirementStrategyService;

  @Resource
  PFEPService pFEPService;

  @RequestMapping(value = "manualRequirement")
  public String manualRequirement(Model model, String navTabId) {
    List<WorkCenter> workcenters = workCenterService.getAllWorkCenterListBySiteCode(getSiteCode(), 0);
    model.addAttribute("navTabId", navTabId);
    model.addAttribute("workcenters", workcenters);
    return "mm/manualRequirement/manualRequirement";
  }

  @RequestMapping(value = "saveManualRequirement")
  @ResponseBody
  public DWZResult saveManualRequirement(Model model, String navTabId, String id, Requirement requirement,
      HttpServletRequest request) {
    if (requirement == null) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }
    requirement.setSiteCode(getSiteCode());
    if (!requirementStrategyService.isPullStrategyEnable()){
      return ajaxError(getImeViewMessageI18n(null, "pullStrategyUnEnable.M"));
    }
    String num = requirementStrategyService.insertManualRequirement(requirement);
    return ajaxDone(num);

  }

  @RequestMapping(value = "mtChange")
  @ResponseBody
  public Requirement mtChange(Model model, String navTabId, String id, String materialCode, HttpServletRequest request) {
    Requirement requirement = new Requirement();
    if (!StringUtils.isEmpty(materialCode)) {
      PFEP pfep = pFEPService.selectPFEP(getSiteCode(), materialCode);
      if (pfep != null) {
        requirement
            .setExpectedArrivalDateTime(new Date(new Date().getTime() + (long) (pfep.getDistributionCycle() * 60000)));
        requirement.setRequirementQuantity(pfep.getDistributionQuantity());
      }
    }

    return requirement;
  }
}
